﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 配表的地图信息
/// </summary>
public struct MapInfoModel
{
    ///<summary>
    ///地图的单元格信息，对应角色坐标为[x,z]
    ///</summary>
    public string[,] Grid;

    ///<summary>
    /// 地图的宽高 0 宽 1 高
    ///</summary>
    public int[] MapSize;

    public MapInfoModel(string[,] grid)
    {
        this.Grid = grid;
        this.MapSize = new []{grid.GetLength(0),grid.GetLength(1)};
    }
}

/// <summary>
/// 移动方式,移动类型和移动消耗
/// </summary>
public struct MovementInfoModel
{
    public MoveType MoveType;
    public int MoveCost;

    public MovementInfoModel(MoveType moveType, int moveCost)
    {
        MoveType = moveType;
        MoveCost = moveCost;
    }
}

//地图的每个单元格的信息
public struct GridInfo{
    ///<summary>
    ///位于resources/prefabs文件夹下的的位置
    ///比如是resources/prefabs/terrain/grass，这个值就是"terrain/grass"
    ///</summary>
    public string PrefabPath;

    /// <summary>
    /// 移动类型信息
    /// </summary>
    public MovementInfoModel[] MovementInfo;

    /// <summary>
    /// 构造一个地图块
    /// </summary>
    /// <param name="prefabPath">资源地址</param>
    public GridInfo(string prefabPath, MovementInfoModel[] movementInfo)
    {
        PrefabPath = prefabPath;
        MovementInfo = movementInfo;
    }

    public static GridInfo VoidGrid{get;} = new GridInfo("DefineMap",null);
}

//整个地图的信息
public class MapInfo{
    ///<summary>
    ///地图的单元格信息，对应角色坐标为[x,z]
    ///</summary>
    public GridInfo[,] Grid;

    ///<summary>
    ///每个单元格的宽度和高度
    ///单位：米
    ///</summary>
    public Vector2 GridSize{get;}

    ///<summary>
    ///获得地图的边界，单位：米
    ///</summary>
    public Rect Border{get;}


    public MapInfo(GridInfo[,] map, Vector2 gridSize){
        Grid = map;
        GridSize = new Vector2(
            Mathf.Max(0.1f, gridSize.x),    //最小0.1米
            Mathf.Max(0.1f, gridSize.y)
        );
        this.Border = new Rect(
            -gridSize.x / 2.00f,
            -gridSize.y / 2.00f,
            gridSize.x * MapWidth(),
            gridSize.y * MapHeight()
        );
    }

    

    ///<summary>
    ///地图的宽度
    ///<return>单位：单元格</return>
    ///</summary>
    public int MapWidth(){
        return Grid.GetLength(0);
    }

    ///<summary>
    ///地图的高度
    ///<return>单位：单元格</return>
    ///</summary>
    public int MapHeight(){
        return Grid.GetLength(1);
    }
    
    ///<summary>
    ///获取某个地图的移动力消耗
    ///<return></return>
    ///</summary>
    public int GetMapCost(Vector2Int pos,MoveType moveType = MoveType.Walk)
    {
        int MOV = 99;
        var gridInfo = Grid[pos.x, pos.y];
        foreach (var movementInfoModel in gridInfo.MovementInfo)
        {
            if (movementInfoModel.MoveType == moveType)
            {
                return movementInfoModel.MoveCost;
            }
        }
        return MOV;
    }

    ///<summary>
    ///获得某个东西坐落在的位置信息
    ///<param name="pos">要检查的点，单位：米</param>
    ///<return>返回这个点的单元格信息</return>
    ///</summary>
    public GridInfo GetGridInPosition(Vector3 pos){
        Vector2Int gPos = GetGridPosByMeter(pos.x, pos.z);
        if (gPos.x < 0 || gPos.x >= MapWidth() || gPos.y < 0 || gPos.y >= MapHeight()) 
            return GridInfo.VoidGrid;
        return Grid[gPos.x, gPos.y];
    }
    
    /// <summary>
    /// 检查坐标是否在地图上
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public bool CheckGridInPosition(Vector2 pos){
        Vector2Int gPos = GetGridPosByMeter(pos.x, pos.y);
        if (gPos.x < 0 || gPos.x >= MapWidth() || gPos.y < 0 || gPos.y >= MapHeight()) 
            return false;
        return true;
    }

    ///<summary>
    ///从地图上x坐标的米，获得单元格坐标x
    ///<param name="x">角色坐标x，单位：米</param>
    ///<param name="z">角色坐标z，单位：米</param>
    ///<return>单元格坐标，单位：单元格</return>
    ///</summary>
    public Vector2Int GetGridPosByMeter(float x, float z){
        return new Vector2Int(
            //Mathf.FloorToInt((x + gridSize.x / 2.00f) / gridSize.x),
            //Mathf.FloorToInt((z + gridSize.y / 2.00f) / gridSize.y)
            Mathf.RoundToInt(x / GridSize.x),
            Mathf.RoundToInt(z / GridSize.y)
        );
    }
    
    ///<summary>
    ///从单元格的坐标的米，获得地图坐标
    ///<param name="gridX">角色坐标x，单位：米</param>
    ///<param name="gridY">角色坐标z，单位：米</param>
    ///<return>地图坐标</return>
    ///</summary>
    public Vector3 GetPosByGridPos(int gridX, int gridY){
        if (gridX < 0 || gridX >= MapWidth() || gridY < 0 || gridY >= MapHeight()) return new Vector3(999,0,999);
        return new Vector3(
            gridX * GridSize.x-GridSize.x / 2.00f,
            gridY * GridSize.y-GridSize.y / 2.00f,
            0
            
        );
    }
    
    /// <summary>
    /// 从单元格的坐标的米，获得地图坐标,不检查边界
    /// </summary>
    /// <param name="gridX"></param>
    /// <param name="gridY"></param>
    /// <returns></returns>
    public Vector3 GetPosByGridPosNoCheck(int gridX, int gridY){
        return new Vector3(
            gridX * GridSize.x-GridSize.x / 2.00f,
            gridY * GridSize.y-GridSize.y / 2.00f,
            0
            
        );
    }
}